/*
 * Copyright (C) 2016 volders GmbH with <3 in Berlin
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package berlin.volders.badger;


import ohos.agp.components.Image;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.ShapeElement;

/**
 * Utility to wrap a drawable with a badge on it.
 */
@SuppressWarnings("WeakerAccess")
public class Badger<T extends BadgeDrawable> {

    /**
     * The final badged drawable {@link #badge} or a {@link }
     */
    public final ShapeElement drawable;
    /**
     * The badge drawable
     */
    public final T badge;

    private Badger(ShapeElement drawable, T badge) {
        this.drawable = drawable;
        this.badge = badge;
    }

    /**
     * Wraps any drawable with a badge
     *
     * @param drawable to add the badge to
     * @param factory  creating a new badge
     * @param <T>      the subtype of {@code BadgeDrawable} to create
     * @return the {@code Badger} holding the
     */
    public static <T extends BadgeDrawable> Badger<T>
    sett(Element drawable, BadgeDrawable.Factory<? extends T> factory) {
//        if (!(drawable instanceof LayerDrawable)) {
//            T badge = factory.createBadge();
//            LayerDrawable layer = new LayerDrawable(new Drawable[]{drawable, badge});
//            layer.setId(1, R.id.badger_drawable);
//            return new Badger<>(drawable, badge);
//        }
//        LayerDrawable layer = (ElementContainer) drawable;

//        drawable = layer.findDrawableByLayerId(R.id.badger_drawable);
//        if (drawable == null) {
//            T badge = factory.createBadge();
//            int count = layer.getNumberOfLayers();
//            Drawable[] layers = new Drawable[count + 1];
//            for (int i = 0; i < count; i++) {
//                layers[i] = layer.getDrawable(i);
//            }
//            layers[count] = badge;
//            layer = new LayerDrawable(layers);
//            layer.setId(count, R.id.badger_drawable);
//            return new Badger<>(drawable, badge);
//        }
        try {
            //noinspection unchecked
            return new Badger<>((ShapeElement) drawable, (T) drawable);
        } catch (ClassCastException e) {
            String errorMessage = "layer with id R.id.badger_drawable must be an instance of "
                    + factory.createBadge().getClass().getSimpleName();
            throw new IllegalStateException(errorMessage);
        }
    }

//    /**
//     * Wraps the icon of a menu item with a badge
//     *
//     * @param item    to add the badge to
//     * @param factory creating a new badge
//     * @param <T>     the subtype of {@code BadgeDrawable} to create
//     * @return the badge drawable
//     */
//    public static <T extends BadgeDrawable> T
//    sett(MenuItem item, BadgeDrawable.Factory<? extends T> factory) {
//        Badger<T> badger = sett(item.getIcon(), factory);
//        item.setIcon(badger.drawable);
//        return badger.badge;
//    }

    /**
     * Wraps the image of an {@link Image} with a badge
     *
     * @param view    to add the badge to
     * @param factory creating a new badge
     * @param <T>     the subtype of {@code BadgeDrawable} to create
     * @return the badge drawable
     */
    public static <T extends BadgeDrawable> T
    sett(Image view, BadgeDrawable.Factory<? extends T> factory) {
        Badger<T> badger = sett(view.getImageElement(), factory);
        view.setImageElement(badger.drawable);
        return badger.badge;
    }
}
